<?php
class SearchAction extends Action{
   public function _initialize(){//初始化
		R("Home.Public","top");
		R("Home.Public","bottom");
	}
	public function index(){//搜索
	     /*安全处理*/
		 checkget();
		 checkpost();
		 /*安全处理*/
		 
		 /*初始化变量*/
		 $map = array();
		 $product = M("product");
		 $sysclass = M("sysclass");
		 /*初始化变量*/
		 
		 /*参数处理*/
		 $typeid = $_POST["typeid"]?$_POST["typeid"]:$_GET["typeid"];
		 if(empty($typeid)){
			$typeid = 0; 
		 }else{
			$col_id = getcol_id("product");
			$map["typeid"] = array("in",typeid2child($typeid,"sysclass","col_id = {$col_id}"));
		 }
		 
		 $keyword = $_POST["keyword"]?$_POST["keyword"]:base64_decode($_GET["keyword"]);
		 if(empty($keyword)){
			$keyword = 0; 
		 }else{
			$map["pr_name"] = array("like","%$keyword%");
		 }
		 
		 $small_keyword = $_POST["small_keyword"]?$_POST["small_keyword"]:base64_decode($_GET["small_keyword"]);
		 		 
		 if(empty($small_keyword)){
			$small_keyword = 0; 
		 }else{
			$map["pr_name"] = array("like","%$small_keyword%");
		 }
		 
		 $order = $_GET["order"]?$_GET["order"]:"new"; 
		 if($order == "new"){
			 $order_str = "upload_date desc"; 
		 }else if($order == "price_up"){
			 $order_str = "pr_shop_price asc";
		 }else if($order == "price_down"){
		     $order_str = "pr_shop_price desc";
		 }
		 $this->assign("order",$order);
		 
		 /*参数处理*/
		 
		 /*获取数据*/
		    //跳转url 
		 $seq_url = U("Search/index",array("typeid"=>$typeid,"small_keyword"=>urlencode(base64_encode($small_keyword)),"keyword"=>urlencode(base64_encode($keyword))));
		 
		 $small_keyword_url = U("Search/index",array("typeid"=>$typeid,"keyword"=>urlencode(base64_encode($keyword)),"order"=>$order));
		 
		 $go_url = $div_url = U("Search/index",array("typeid"=>$typeid,"small_keyword"=>urlencode(base64_encode($small_keyword)),"keyword"=>urlencode(base64_encode($keyword)),"order"=>$order));
		 
		 $this->assign("seq_url",$seq_url);
		 $this->assign("small_keyword_url",$small_keyword_url);
		 $this->assign("go_url",$go_url);
		 
		 
		 $pr_count = $product->where($map)->count();
		 $pr_count = $pr_count?$pr_count:0;
		 $this->assign("pr_count",$pr_count);
		 Vendor("LJW.page");
		 $mypage = new page($pr_count,20,3,$div_url);
		   //分页
		     //上一页   下一页 
		 if($mypage->absolutepage <> 1) $prev_page="<span class='next_bz'><a href='".$mypage->url."/p/".($mypage->absolutepage-1)."'>上一页</a></span>";	
		 if($mypage->absolutepage <> $mypage->pagenum) $next_page="<span class='next_bz'><a href='".$mypage->url."/p/".($mypage->absolutepage+1)."'>下一页</a></span>";
		 
		 $start=$mypage->absolutepage-4;
		 if($start<1)$start=1;
		 $end=$start+8;
		 if($end>$mypage->pagenum) $end=$mypage->pagenum;
			 
		 $start_url=$mypage->url."/p/".$start;
		 $end_url=$mypage->url."/p/".$end;
			 
		 $f_url=$mypage->url."/p/1";//第一页
		 $l_url=$mypage->url."/p/".$mypage->pagenum;//最后一页
			 
		 if($start<>1)$f_str="<a href='$f_url'>1</a><a href='#'>…</a>";
		 if($end<>$mypage->pagenum)$l_str="<a href='#'>…</a><a href='$l_url'>".$mypage->pagenum."</a>";
			 
		 for($i=$start;$i<=$end;$i++){
			if($mypage->absolutepage == $i){
			   $m_str.="<a href='".$mypage->url."/p/".$i."' class='on_link' >$i</a>";
			}else{
			   $m_str.="<a href='".$mypage->url."/p/".$i."'>$i</a>";	
			}
			
		 }
		 $div_str.=$prev_page.$f_str.$m_str.$l_str.$next_page;
		 
		 if(!empty($pr_count))$this->assign("divpage",$div_str);
		 
		 $pr_list = $product->where($map)->limit($mypage->start.",".$mypage->pagesize)->order($order_str)->findAll();
		 
		 $this->assign("pr_list",$pr_list);
		/*获取数据*/ 
		
		$this->display(); 
	}
	
}
?>